import { request } from "../../request/index.js";
import regeneratorRuntime from "../../libs/runtime/runtime";

Page({
    /**
     * 页面的初始数据
     */
    data: {
        active: 0, //默认是全部
        allOrderList: [], //所有
    },
    totalPages: 1, // 总页数

    allOrder: {
        page: 1,
        size: 10,
        orderType: "", //订单状态 -1:已取消 0:代付款 1:已付款 2:已发货 3:已完成
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        console.log(options);
        this.setData({
            active: options.type,
        });
        this.allOrder.orderType = this.data.active - 1;
        this.allOrdersPsot();
    },
    async allOrdersPsot() {
        let res = await request({
            url: "BlanketOrder/findUserAllOrder",
            data: this.allOrder,
        });
        console.log(res);
        const total = res.data.total;
        // 计算总页数
        this.totalPages = Math.ceil(total / this.allOrder.size);
        console.log(this.totalPages, "999999999999");

        if (res.code == 200) {
            this.setData({
                allOrderList: [...this.data.allOrderList, ...res.data.records],
            });
        }
    },
    //订单详情
    lineItem(e) {
        console.log(e);

        wx.navigateTo({
            url: "../line_item/line_item?id=" + e.target.dataset.id,
        });
    },
    // 顶部change事件
    onChange(e) {
        if (e.detail.title == "待付款") {
            this.allOrder.orderType = 0;
            this.allOrder.page = 1;
            this.setData({
                allOrderList: [],
            });
            this.allOrdersPsot();
        } else if (e.detail.title == "待发货") {
            this.allOrder.orderType = 1;
            this.allOrder.page = 1;
            this.setData({
                allOrderList: [],
            });
            this.allOrdersPsot();
        } else if (e.detail.title == "待收货") {
            this.allOrder.orderType = 2;
            this.allOrder.page = 1;
            this.setData({
                allOrderList: [],
            });
            this.allOrdersPsot();
        } else if (e.detail.title == "已完成") {
            this.allOrder.orderType = 3;
            this.allOrder.page = 1;
            this.setData({
                allOrderList: [],
            });
            this.allOrdersPsot();
        } else if (e.detail.title == "全部") {
            this.allOrder.orderType = "";
            this.allOrder.page = 1;
            this.setData({
                allOrderList: [],
            });
            this.allOrdersPsot();
        }
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {},

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        // this.allOrdersPsot();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {},

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {},

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {},

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        //  1 判断还有没有下一页数据
        if (this.allOrder.page >= this.totalPages) {
            wx.showToast({
                title: "我是有底线的",
                icon: "none",
            });
        } else {
            this.allOrder.page++;
            this.allOrdersPsot();
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {},
});